package com.huawei.appgallery.forum.base.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.DrawableRes;
import androidx.core.view.ViewCompat;
import java.util.ArrayList;
import java.util.List;
import o.bqx;
import o.bqz.c;
import o.bro;
import o.bro.e;
import o.bro.g;
import o.brp;
import o.brq;
import o.fo;
import o.iv;

public class ForumPostImageView extends View implements g {
    private float ʻ;
    private List<e> ʻॱ;
    private brp ʼ;
    private float ʽ;
    private final iv ˊ;
    private int ˊॱ;
    private final bro ˋ;
    private float ˋॱ;
    private final Paint ˎ;
    protected Drawable ˏ;
    private brq ˏॱ;
    private g ͺ;
    private int ॱ;
    private Drawable ॱˊ;
    private Rect ॱˋ;
    private boolean ॱˎ;
    private int ॱॱ;
    private float ᐝ;

    public ForumPostImageView(Context context) {
        this(context, null);
    }

    public ForumPostImageView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public ForumPostImageView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.ʽ = 1.0f;
        this.ʻॱ = new ArrayList();
        this.ॱˋ = new Rect();
        this.ˊ = iv.ˎ(getContext(), null);
        this.ˏॱ = new brq();
        setFocusable(true);
        setWillNotDraw(false);
        this.ˋ = new bro(context);
        this.ˋ.ˏ((g) this);
        this.ˎ = new Paint();
        this.ˎ.setColor(-7829368);
        this.ˎ.setAntiAlias(true);
        this.ˏ = getResources().getDrawable(c.ˏ);
    }

    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (!isInEditMode() && this.ˏ != null) {
            this.ˏ.setBounds(0, 0, getWidth(), getHeight());
            this.ˏ.draw(canvas);
        }
    }

    protected void drawableStateChanged() {
        if (this.ˏ != null) {
            this.ˏ.setState(getDrawableState());
            invalidate();
        }
        super.drawableStateChanged();
    }

    public void computeScroll() {
        super.computeScroll();
        if (this.ˏॱ.ˏ()) {
            setScale(this.ˏॱ.ॱ(), this.ˏॱ.ˊ(), this.ˏॱ.ˎ());
        }
        if (this.ˊ.ॱ()) {
            int scrollX = getScrollX();
            int scrollY = getScrollY();
            int ˏ = this.ˊ.ˏ();
            int ˋ = this.ˊ.ˋ();
            if (!(scrollX == ˏ && scrollY == ˋ)) {
                int i = ˏ - scrollX;
                ˋ -= scrollY;
                ˏ(i, ˋ, scrollX, scrollY, ʼ(), ˎ(), 0, 0, false);
            }
            if (!this.ˊ.ˊ()) {
                ʽ();
            }
        }
    }

    public boolean canScrollHorizontally(int i) {
        if (i > 0) {
            if (getScrollX() < ʼ()) {
                return true;
            }
            return false;
        } else if (getScrollX() <= 0 || ʼ() <= 0) {
            return false;
        } else {
            return true;
        }
    }

    public boolean canScrollVertically(int i) {
        if (i > 0) {
            if (getScrollY() < ˎ()) {
                return true;
            }
            return false;
        } else if (getScrollY() <= 0 || ˎ() <= 0) {
            return false;
        } else {
            return true;
        }
    }

    public void setOnImageLoadListener(g gVar) {
        this.ͺ = gVar;
    }

    public void setImage(Bitmap bitmap) {
        setImageDrawable(new BitmapDrawable(getResources(), bitmap));
    }

    public void setImage(Drawable drawable) {
        setImageDrawable(drawable);
    }

    public void setImage(@DrawableRes int i) {
        setImageDrawable(fo.ˊ(getContext(), i));
    }

    public void setImageDrawable(Drawable drawable) {
        this.ʼ = null;
        this.ʽ = 1.0f;
        scrollTo(0, 0);
        if (this.ॱˊ != drawable) {
            int i = this.ॱ;
            int i2 = this.ॱॱ;
            ˏ(drawable);
            ˊ(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            if (!(i == this.ॱ && i2 == this.ॱॱ)) {
                requestLayout();
            }
            ʽ();
        }
    }

    public void setImage(brp o_brp) {
        setImage(o_brp, null);
    }

    public void setImage(brp o_brp, Drawable drawable) {
        this.ʽ = 1.0f;
        this.ʼ = o_brp;
        scrollTo(0, 0);
        ˏ(drawable);
        this.ˋ.ˊ(o_brp);
        invalidate();
    }

    private void ˏ(Drawable drawable) {
        boolean z = false;
        if (this.ॱˊ != null) {
            this.ॱˊ.setCallback(null);
            unscheduleDrawable(this.ॱˊ);
            if (this.ॱˎ) {
                this.ॱˊ.setVisible(false, false);
            }
        }
        this.ॱˊ = drawable;
        if (drawable != null) {
            drawable.setCallback(this);
            if (VERSION.SDK_INT >= 23) {
                drawable.setLayoutDirection(getLayoutDirection());
            }
            if (drawable.isStateful()) {
                drawable.setState(getDrawableState());
            }
            if (this.ॱˎ) {
                if (getWindowVisibility() == 0 && isShown()) {
                    z = true;
                }
                drawable.setVisible(z, true);
            }
            drawable.setLevel(this.ˊॱ);
            this.ॱ = drawable.getIntrinsicWidth();
            this.ॱॱ = drawable.getIntrinsicHeight();
            return;
        }
        this.ॱॱ = -1;
        this.ॱ = -1;
    }

    public boolean ˋ() {
        if (this.ॱˊ != null) {
            return true;
        }
        if (this.ʼ != null) {
            return this.ˋ.ˎ();
        }
        return false;
    }

    public int computeVerticalScrollRange() {
        int height = (getHeight() - getPaddingBottom()) - getPaddingTop();
        int ʻ = ʻ();
        int scrollY = getScrollY();
        height = Math.max(0, ʻ - height);
        if (scrollY < 0) {
            return ʻ - scrollY;
        }
        if (scrollY > height) {
            return ʻ + (scrollY - height);
        }
        return ʻ;
    }

    public int ॱ() {
        if (this.ॱˊ != null) {
            return this.ॱ;
        }
        if (this.ʼ == null || !ˋ()) {
            return 0;
        }
        return this.ॱ;
    }

    public int ˏ() {
        if (this.ॱˊ != null) {
            return this.ॱॱ;
        }
        if (this.ʼ == null || !ˋ()) {
            return 0;
        }
        return this.ॱॱ;
    }

    private int ˎ() {
        return ʻ() - ((getHeight() - getPaddingBottom()) - getPaddingTop());
    }

    public int computeVerticalScrollOffset() {
        return Math.max(0, super.computeVerticalScrollOffset());
    }

    public int computeVerticalScrollExtent() {
        return super.computeVerticalScrollExtent();
    }

    public int computeHorizontalScrollRange() {
        int width = (getWidth() - getPaddingRight()) - getPaddingLeft();
        int ॱॱ = ॱॱ();
        int scrollX = getScrollX();
        width = Math.max(0, ॱॱ - width);
        if (scrollX < 0) {
            return ॱॱ - scrollX;
        }
        if (scrollX > width) {
            return ॱॱ + (scrollX - width);
        }
        return ॱॱ;
    }

    private int ʼ() {
        return ॱॱ() - ((getWidth() - getPaddingRight()) - getPaddingLeft());
    }

    private int ॱॱ() {
        if (ˋ()) {
            return (int) (((float) getMeasuredWidth()) * this.ʽ);
        }
        return 0;
    }

    private int ʻ() {
        if (!ˋ() || ॱ() == 0) {
            return 0;
        }
        return (int) ((((1.0f * ((float) getMeasuredWidth())) * ((float) ˏ())) / ((float) ॱ())) * this.ʽ);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        if (width != 0) {
            int i;
            int i2;
            int ॱॱ = ॱॱ();
            int ʻ = ʻ();
            if (width > ॱॱ) {
                i = (width - ॱॱ) / 2;
            } else {
                i = 0;
            }
            if (height > ʻ) {
                i2 = (height - ʻ) / 2;
            } else {
                i2 = 0;
            }
            if (this.ʼ != null) {
                int scrollX = getScrollX();
                width += scrollX;
                int scrollY = getScrollY();
                int i3 = scrollY + height;
                float ˋ = ((float) this.ˋ.ˋ()) / (this.ʽ * ((float) getWidth()));
                this.ॱˋ.left = (int) Math.ceil((double) (((float) (scrollX - 0)) * ˋ));
                this.ॱˋ.top = (int) Math.ceil((double) (((float) (scrollY - 0)) * ˋ));
                this.ॱˋ.right = (int) Math.ceil((double) (((float) (width - 0)) * ˋ));
                this.ॱˋ.bottom = (int) Math.ceil((double) (((float) (i3 - 0)) * ˋ));
                scrollX = canvas.save();
                try {
                    ˏ(canvas, i, i2, ˋ, 0, 0);
                } catch (RuntimeException e) {
                    bqx.ॱ("ForumPostImageView", "canvas draw exception!");
                }
                canvas.restoreToCount(scrollX);
            } else if (this.ॱˊ != null) {
                this.ॱˊ.setBounds(i, i2, i + ॱॱ, i2 + ʻ);
                this.ॱˊ.draw(canvas);
            }
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void ˏ(android.graphics.Canvas r9, int r10, int r11, float r12, int r13, int r14) {
        /*
        r8 = this;
        r0 = r8.getResources();
        r0 = r0.getDisplayMetrics();
        r1 = r8.ॱˊ;
        if (r1 == 0) goto L_0x0028;
    L_0x000c:
        r1 = r8.ˋ;
        r1 = r1.ˎ();
        if (r1 == 0) goto L_0x0028;
    L_0x0014:
        r1 = r8.ˋ;
        r1 = r1.ˋ();
        r2 = r8.ˋ;
        r2 = r2.ˊ();
        r1 = r1 * r2;
        r2 = r0.widthPixels;
        r0 = r0.heightPixels;
        r0 = r0 * r2;
        if (r1 <= r0) goto L_0x0031;
    L_0x0028:
        r0 = r8.ˋ;
        r1 = r8.ʻॱ;
        r2 = r8.ॱˋ;
        r0.ॱ(r1, r12, r2);
    L_0x0031:
        r0 = r8.ʻॱ;
        r0 = r0.isEmpty();
        if (r0 == 0) goto L_0x0052;
    L_0x0039:
        r0 = r8.ॱˊ;
        if (r0 == 0) goto L_0x0051;
    L_0x003d:
        r0 = r8.ॱˊ;
        r1 = r8.ॱॱ();
        r1 = r1 + r10;
        r2 = r8.ʻ();
        r2 = r2 + r11;
        r0.setBounds(r10, r11, r1, r2);
        r0 = r8.ॱˊ;
        r0.draw(r9);
    L_0x0051:
        return;
    L_0x0052:
        r0 = r8.ʻॱ;
        r1 = r0.iterator();
    L_0x0058:
        r0 = r1.hasNext();
        if (r0 == 0) goto L_0x0051;
    L_0x005e:
        r0 = r1.next();
        r0 = (o.bro.e) r0;
        r2 = r0.ˊ;
        r3 = r2.left;
        r3 = (float) r3;
        r3 = r3 / r12;
        r4 = (double) r3;
        r4 = java.lang.Math.ceil(r4);
        r6 = (double) r13;
        r4 = r4 + r6;
        r3 = (int) r4;
        r3 = r3 + r10;
        r2.left = r3;
        r3 = r2.top;
        r3 = (float) r3;
        r3 = r3 / r12;
        r4 = (double) r3;
        r4 = java.lang.Math.ceil(r4);
        r6 = (double) r14;
        r4 = r4 + r6;
        r3 = (int) r4;
        r3 = r3 + r11;
        r2.top = r3;
        r3 = r2.right;
        r3 = (float) r3;
        r3 = r3 / r12;
        r4 = (double) r3;
        r4 = java.lang.Math.ceil(r4);
        r6 = (double) r13;
        r4 = r4 + r6;
        r3 = (int) r4;
        r3 = r3 + r10;
        r2.right = r3;
        r3 = r2.bottom;
        r3 = (float) r3;
        r3 = r3 / r12;
        r4 = (double) r3;
        r4 = java.lang.Math.ceil(r4);
        r6 = (double) r14;
        r4 = r4 + r6;
        r3 = (int) r4;
        r3 = r3 + r11;
        r2.bottom = r3;
        r3 = r0.ॱ;
        r0 = r0.ˎ;
        r4 = 0;
        r9.drawBitmap(r3, r0, r2, r4);
        goto L_0x0058;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.huawei.appgallery.forum.base.image.ForumPostImageView.ˏ(android.graphics.Canvas, int, int, float, int, int):void");
    }

    public void ˊ() {
        ʽ();
        if (this.ͺ != null) {
            this.ͺ.ˊ();
        }
    }

    public void ˊ(final int i, final int i2) {
        this.ॱ = i;
        this.ॱॱ = i2;
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        if (measuredWidth == 0 || measuredHeight == 0) {
            post(new Runnable(this) {
                final /* synthetic */ ForumPostImageView ॱ;

                public void run() {
                    this.ॱ.ˋ(i, i2);
                }
            });
        } else {
            ˋ(i, i2);
        }
        ʽ();
        if (this.ͺ != null) {
            this.ͺ.ˊ(i, i2);
        }
    }

    public void ˊ(Exception exception) {
        if (this.ͺ != null) {
            this.ͺ.ˊ(exception);
        }
    }

    private void ˋ(int i, int i2) {
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        if (i > i2) {
            this.ᐝ = (((float) measuredHeight) * ((((float) i) * 1.0f) / ((float) measuredWidth))) / ((float) i2);
            this.ʻ = ((((float) i) * 1.0f) / ((float) measuredWidth)) * 4.0f;
            this.ˋॱ = ((((float) i) * 1.0f) / ((float) measuredWidth)) / 4.0f;
            if (this.ˋॱ > 1.0f) {
                this.ˋॱ = 1.0f;
                return;
            }
            return;
        }
        this.ᐝ = 1.0f;
        this.ˋॱ = 0.25f;
        this.ʻ = (((float) i) * 1.0f) / ((float) measuredWidth);
        float f = (((((float) i) * 1.0f) / ((float) measuredWidth)) * ((float) measuredHeight)) / ((float) i2);
        this.ʻ = getContext().getResources().getDisplayMetrics().density * this.ʻ;
        if (this.ʻ < 4.0f) {
            this.ʻ = 4.0f;
        }
        if (this.ˋॱ > f) {
            this.ˋॱ = f;
        }
    }

    private void ʽ() {
        ViewCompat.ˎ(this);
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.ॱˎ = false;
        if (this.ॱˊ != null) {
            boolean z;
            Drawable drawable = this.ॱˊ;
            if (getVisibility() == 0) {
                z = true;
            } else {
                z = false;
            }
            drawable.setVisible(z, false);
        }
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.ॱˎ = true;
        this.ˋ.ˏ();
        if (this.ॱˊ != null) {
            this.ॱˊ.setVisible(false, false);
        }
    }

    private void ˏ(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8, boolean z) {
        boolean z2;
        int i9;
        boolean z3;
        int i10 = i3 + i;
        int i11 = i4 + i2;
        int i12 = -i7;
        int i13 = i7 + i5;
        int i14 = -i8;
        int i15 = i8 + i6;
        if (i10 > i13) {
            z2 = true;
        } else if (i10 < i12) {
            i13 = i12;
            z2 = true;
        } else {
            z2 = false;
            i13 = i10;
        }
        if (i11 > i15) {
            i9 = i15;
            z3 = true;
        } else if (i11 < i14) {
            z3 = true;
            i9 = i14;
        } else {
            z3 = false;
            i9 = i11;
        }
        if (i13 < 0) {
            i13 = 0;
        }
        if (i9 < 0) {
            i9 = 0;
        }
        onOverScrolled(i13, i9, z2, z3);
    }

    protected void onOverScrolled(int i, int i2, boolean z, boolean z2) {
        super.scrollTo(i, i2);
    }

    protected void onSizeChanged(int i, int i2, int i3, int i4) {
        super.onSizeChanged(i, i2, i3, i4);
        if (ˋ()) {
            ˋ(this.ॱ, this.ॱॱ);
        }
    }

    public void setScale(float f) {
        setScale(f, getMeasuredWidth() >> 1, getMeasuredHeight() >> 1);
    }

    public void setScale(float f, int i, int i2) {
        if (ˋ()) {
            float f2 = this.ʽ;
            this.ʽ = f;
            int scrollX = getScrollX();
            int scrollY = getScrollY();
            ˏ((int) (((float) (scrollX + i)) * ((f / f2) - 1.0f)), (int) (((f / f2) - 1.0f) * ((float) (scrollY + i2))), scrollX, scrollY, ʼ(), ˎ(), 0, 0, false);
            ʽ();
        }
    }

    public void setImageUrl(String str) {
        if (this.ˋ != null) {
            this.ˋ.ˊ(str);
        }
    }
}
